package com.atguigu.utils;

import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @className: DateFormatUtil
 * @author: LinCong
 * @description:
 * @date: 2023/1/16 22:19
 * @version: 1.0
 */
public class DateFormatUtil {
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static Long toTs(String dtStr, boolean isFull) {

        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00";
        }
        localDateTime = LocalDateTime.parse(dtStr, dtfFull);

        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toTs(String dtStr) {
        return toTs(dtStr, false);
    }

    public static String toDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtf.format(localDateTime);
    }

    public static String toYmdHms(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtfFull.format(localDateTime);
    }

    public static void main(String[] args) throws ParseException {
//        Date date1 = new Date(1673880068492L);
        Date date0 = new Date(1673880068492L);
        System.out.println(date0.toInstant());  // 2023-01-16T14:41:08.492Z
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());
        System.out.println(date.toString());
        System.out.println("====================");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = "2012-01-29 22:26:23";
//        将日期字符串解析成日期
        Date date1 = sdf.parse(s);
        System.out.println(date1);  // Sun Jan 29 22:26:23 CST 2012
//        将日期解析成给定格式日期字符串
        String s1 = sdf.format(1673880068492L);
        System.out.println(s1); // 2023-01-16 22:41:08
        String s2 = sdf.format(date);
        System.out.println(s2); // 2023-01-17 16:17:12
        System.out.println("====================");
        System.out.println(toYmdHms(System.currentTimeMillis())); // 2023-01-17 16:17:12
    }
}
